package com.example.a58;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.List;

import database.ShoppingDBHelper;
import entity.GoodsInfo;

public class ShoppingChannel extends AppCompatActivity {
    private ShoppingDBHelper mHelper;
    private TextView tv_title;
    private GridLayout gl_Channel;
    private TextView tv_count;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_shopping_channel);

        mHelper = ShoppingDBHelper.getInstance(this);
        mHelper.OpenReadLink();
        mHelper.OpenWriteLink();

        tv_title =  findViewById(R.id.tv_title);
        tv_title.setText("手机商城");
        gl_Channel = findViewById(R.id.gl_channel);
        tv_count = findViewById(R.id.tv_count);
        Log.e("TAG", "onCreate: " );
        //从数据库查询信息
        showGoods();




    }

    private void showGoods() {
        Log.e("TAG", "showGoods: ");
        int screenWidth = getResources().getDisplayMetrics().widthPixels;
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(screenWidth / 2, ViewGroup.LayoutParams.WRAP_CONTENT);
        List<GoodsInfo> list = mHelper.queryAllGoodsInfo();
        Log.e("TAG", "showGoods: "+ list.size());
        Log.e("TAG", "showGoods: 12222" );
        for (GoodsInfo info: list){
            View inflate = LayoutInflater.from(this).inflate(R.layout.item_goods, null);
            ImageView iv_thumb = inflate.findViewById(R.id.iv_thumb);
            TextView tv_name = inflate.findViewById(R.id.tv_name);
            TextView tv_price = inflate.findViewById(R.id.tv_price);

            iv_thumb.setImageURI(Uri.parse(info.picPath));
            tv_name.setText(info.name);
            tv_price.setText(String.valueOf((int)info.price));
            Log.e("TAG", "showGoods: "+info);
            gl_Channel.addView(inflate,layoutParams);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHelper.closeLink();
    }
}